<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>menu</title>
    <link rel="stylesheet" href="/layui/lib/layui-v2.5.5/css/layui.css" media="all">
    <link rel="stylesheet" href="/layui/css/public.css" media="all">
    <style>
        .layui-btn:not(.layui-btn-lg ):not(.layui-btn-sm):not(.layui-btn-xs) {
            height: 34px;
            line-height: 34px;
            padding: 0 8px;
        }
    </style>
</head>
<body>
<!--<script type="text/html" id="toolbarDemo">-->
    <!--<div class="layui-btn-container">-->
        <!--<button class="layui-btn layui-btn-normal layui-btn-sm data-add-btn" lay-event="add" id="add"> 添加 </button>-->
        <!--<button class="layui-btn layui-btn-sm layui-btn-danger data-delete-btn" lay-event="delete"> 删除 </button>-->
    <!--</div>-->
<!--</script>-->
<!--<div class="layuimini-container">-->
    <!--<div class="layui-btn-group">-->
        <!--<button class="layui-btn layui-btn-normal layui-btn-sm data-add-btn" lay-event="add" id="add"> 添加 </button>-->
        <!--<button class="layui-btn layui-btn-sm layui-btn-danger data-delete-btn" lay-event="delete"> 删除 </button>-->
    <!--</div>-->
    <!--<table id="munu-add" class="layui-table" lay-filter="munu-table"></table>-->
<!--</div>-->
<div class="layuimini-container">
            <div class="layui-btn-group">
                <button class="layui-btn" id="btn-expand">全部展开</button>
                <button class="layui-btn layui-btn-normal" id="btn-fold">全部折叠</button>
                <button class="layui-btn layui-btn-normal layui-btn-sm data-add-btn" lay-event="add" id="add"> 添加 </button>
            </div>
            <table id="munu-table" class="layui-table" lay-filter="munu-table"></table>
        </div>

<!-- 操作列 -->
<script type="text/html" id="auth-state">
    <a class="layui-btn layui-btn-primary layui-btn-xs" lay-event="edit">修改</a>
    <a class="layui-btn layui-btn-danger layui-btn-xs" lay-event="del">删除</a>
</script>

<script src="/layui/lib/layui-v2.5.5/layui.js" charset="utf-8"></script>
<script src="/layui/js/lay-config.js?v=1.0.4" charset="utf-8"></script>
<script>
    layui.use(['table', 'treetable'], function () {
        var $ = layui.jquery;
        var table = layui.table;
        var treetable = layui.treetable;

        // 渲染表格
        layer.load(2);
        treetable.render({
            treeColIndex: 1,
            treeSpid: 0,
            treeIdName: 'id',
            treePidName: 'pid',
            elem: '#munu-table',
            url: '/admin/auth_table',
            page: false,
            cols: [[
                {type: 'numbers'},
                {field: 'title', minWidth: 200, title: '权限名称'},
                {field: 'authority', title: '权限标识'},
                {field: 'href', title: '菜单url'},
                {field: 'icon',align: 'center', title: '权限图标'},
                {
                    field: 'isMenu', width: 80, align: 'center', templet: function (d) {
                        if (d.is_menu== 1) {
                            return '<span class="layui-badge layui-bg-gray">按钮</span>';
                        } else {
                            return '<span class="layui-badge-rim">菜单</span>';
                        }
                    }, title: '类型'
                },
                {templet: '#auth-state', width: 120, align: 'center', title: '操作'}
            ]],
            done: function () {
                layer.closeAll('loading');
            }
        });

        $('#btn-expand').click(function () {
            treetable.expandAll('#munu-table');
        });

        $('#btn-fold').click(function () {
            treetable.foldAll('#munu-table');
        });

        //监听工具条
        table.on('tool(munu-table)', function (obj) {
            var data = obj.data;
            var layEvent = obj.event;

            if (layEvent === 'del') {
                var id=data.id;
                layer.confirm('真的删除该权限吗', function (index) {
                    obj.del();
                    layer.close(index);
                });
            } else if (layEvent === 'edit') {
                var auth_id=12;
                $.post("/admin/auth_user",{auth_id},function(res) {
                    if (res == 1) {
                        layer.open({
                            type: 2,
                            title: '修改权限',
                            shadeClose: true,
                            shade: 0.8,
                            area: ['40%', '70%'],
                            content: '/admin/auth_edit/' + data.id
                        });
                    }else{
                        layer.msg('你没有权限');
                    }
                });
            }
        });
        //监听添加
        $("#add").click(function func(){
            var auth_id=5;
            $.post("/admin/auth_user",{auth_id},function(res) {
                if (res == 1) {
                    layer.open({
                        type: 2,
                        title: '添加权限',
                        shadeClose: true,
                        shade: 0.8,
                        area: ['40%', '70%'],
                        content: '/admin/auth_add'
                    });
                }else{
                    layer.msg('你没有权限');
                }
            });
            });
    });
</script>
</body>
</html>